/*
 * 10.3_1.cpp 2533_poj
 *
 *  Created on: 2012-5-16
 *      Author: jawinton
 */


#include <cstdio>
#include <algorithm>

using namespace std;

#define MAX 1001

int arr[MAX];
int maxLen[MAX];
int line[MAX];

int main (void) {
	int n;
	scanf("%d", &n);
	for (int i=0; i<n; i++) {
		scanf("%d", &arr[i]);
		maxLen[i] = -1;
	}

	maxLen[0] = 1;

	for (int i=1; i<n; i++) {
		int max = 0;
		for (int j=0; j<i; j++) {
			if (arr[i] > arr[j]) {
				int tmp = maxLen[j];
				if (max < tmp) {
					max = tmp;
					line[i] = j;
				}
			}
		}
		maxLen[i] = max + 1;
	}
	int nMax = -1;
	int k;
	for (int i=0; i<n; i++) {
		if (nMax < maxLen[i]) {
			nMax = maxLen[i];
			k = i;
		}
	}
	printf("%d\n", nMax);
	for (int i=k; i>0; i=line[i]) {
		printf("%d ", arr[i]);
	}
	printf("%d\n", arr[0]);
	return 0;
}
